package com.rem.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 发送数据到kafka
 * 同步
 *
 * @author Rem
 * @date 2022-12-28
 */
public class CustomerProducerSync {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties properties = new Properties();
        //连接kafka
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9093,localhost:9094,localhost:9095");
        //配置kafka连接器以及key value序列化方式
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties, new StringSerializer(), new StringSerializer());

        for (int i = 0; i < 5; i++) {
            Future<RecordMetadata> first = kafkaProducer.send(new ProducerRecord<>("first", "hello" + i));
            first.get();
        }
        kafkaProducer.close();
    }
}